<template>
  <div>
    <a-card class="card-header">
      <a-form-model :model="queryParam">
        <a-row :gutter="24">
          <a-col :span="6">
            <a-form-model-item
              label="验收单号"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-input
                v-model.trim="queryParam.acceptanceSheetNum"
                placeholder="请输入验收单号"
              ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="2">
            <a-button
              type="primary"
              style="margin-top:3px"
              @click="handleSearch"
              >查询</a-button
            >
          </a-col>
        </a-row>
      </a-form-model>
    </a-card>

    <a-card class="card-content">
      <a-row :gutter="24">
        <a-col :span="22">
          <a-button type="primary" @click="handleAdd">新建</a-button>
          <!-- <a-button type="primary" @click="handleAdd">导出</a-button> -->
          <!-- <a-button type="primary" @click="handleAdd">导入</a-button> -->
        </a-col>
        <a-col :span="2">
          <a-button
            type="primary"
            @click="handleAllList('ReceiptSheetFindAllByPage')"
            >全部验收单</a-button
          >
        </a-col>
      </a-row>
      <br />
      <a-table
        :columns="columns"
        :data-source="tableData"
        :loading="loading"
        :pagination="pagination"
        @change="handleTableChange"
        bordered
        :rowKey="(record, index) => record.id"
      >
        <template slot="wareHouseName" slot-scope="text, record">
          {{ getWareHouseName(record) }}
        </template>

        <template slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">编辑</a>
          <a-divider type="vertical" />
          <a-popconfirm
            okText="确定"
            cancelText="取消"
            v-if="tableData.length"
            title="确定删除吗?"
            @confirm="() => handleDelete(record)"
          >
            <a>删除</a>
          </a-popconfirm>
        </template>
      </a-table>
    </a-card>
  </div>
</template>

<script>
import { columns } from "./components/tableConfig";
import { tableMinxins } from "@/unit/mixins/tableMinxins.js";
import api from "@/api/apiServer";
export default {
  components: {},
  mixins: [tableMinxins],
  data() {
    return {
      columns,
      tableData: [],
      wareHouseList: [], // 仓库列表
      goodsList: [], // 商品列表
    };
  },
  mounted() {
    this.getWareHouseList();
    // this.getGoodsList();
    this.getList();
  },
  methods: {
    // 获取备料单列表数据
    async getList() {
      var params = this.getQueryParams();
      const res = await api.ReceiptSheetFindAllByPage(params);
      this.tableData = res.data.data;
    },

    // 获取仓库列表
    async getWareHouseList() {
      var params = {};
      const res = await api.wareHouseList(params);
      this.wareHouseList = res.data;
    },

    // 获取商品列表
    async getGoodsList() {
      var params = {};
      const res = await api.GoodsFindAll(params);
      this.goodsList = res.data;
    },

    // 获取仓库名称
    getWareHouseName(record) {
      for (var item of this.wareHouseList) {
        if (item.id == record.wareHouseId) {
          return item.wareHouseName;
        }
      }
    },

    // 删除
    async handleDelete(record) {
      const res = await api.ReceiptSheetDelete({
        id: record.id,
      });
      if (res.code && res.code !== 200) {
        this.$message.error(res.message);
        return;
      }
      this.$message.success(res.message);
      const tableData = [...this.tableData];
      this.tableData = tableData.filter((item) => item.id !== record.id);
    },

    // 跳转到新建备料页面
    handleAdd() {
      this.$router.push("/checksheetadd");
    },

    // 编辑
    handleEdit(record) {
      this.$router.push({
        path: "/checksheetadd",
        query: { record: JSON.stringify(record) },
      });
    },
  },
};
</script>

<style lang="less" scoped>
.card-header {
  margin: 20px 0;
}
.card-content {
  button {
    margin-right: 20px;
  }
}
::v-deep .ant-form-item {
  margin-bottom: 0;
}
</style>
